package com.kuaixueit.crm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.kuaixueit.crm.pojo.ResultVO;
import com.kuaixueit.crm.service.RoleService;

@RequestMapping("role")
@Controller
public class RoleController {
	
	@Autowired
	private RoleService roleServcie;
	
	
	
	/**
	 * 分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody 
	public ResultVO list(
			@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="3")Integer rows) {
		PageInfo  info = new PageInfo();
		
		try {
			info = roleServcie.findPageRole(page,rows);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO("查询出错");
		}
		return new ResultVO(Integer.valueOf(""+info.getTotal()), info.getList(), "查询成功");
	}
	
	/**
	 * 格局角色id查询角色权限
	 * @param roleId
	 * @return
	 */
	@RequestMapping("funs")
	@ResponseBody 
	public List list(
			@RequestParam(required=true)Integer roleId) {
		List  list = new ArrayList();
		
		try {
			list = roleServcie.findFunctionByRoleId(roleId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	/**
	 * 修改角色权限
	 * @param roleId
	 * @param fids
	 * @return
	 */
	
	@RequestMapping("authfuns")
	@ResponseBody 
	public ResultVO autoFunction(
			@RequestParam(required=true)Integer roleId,
			@RequestParam(required=true)Integer[] fids
			) {
		try {
			 roleServcie.insertFunnctionsByRoid(roleId, fids);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO("修改失败");
		}
		return new ResultVO("修改成功");
		
	}

}
